package edu.gdkm.controller;

import edu.gdkm.annotation.LoginInterceptor;
import edu.gdkm.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @PostMapping("/login")
    public String login(String username,String password){ //简单数据类型的绑定，同名即可
        //username和password都是root，则跳转到success.jsp,否则，返回原页面
        if("root".equals(username)&&"root".equals(password)){
            return "success";
        }else{
            return "redirect:/login.jsp";
        }
    }

    @PostMapping("/loginbyuser")
    public String login(User user, HttpSession session){
        System.out.println(user);
        if("root".equals(user.getUsername())&&"root".equals(user.getPassword())){
            session.setAttribute("user",user);
            return "success";
        }else{
            return "redirect:/login.jsp";
        }
    }
    @LoginInterceptor
    @RequestMapping("/test")
    @ResponseBody
    public User test(@RequestBody User user){
        System.out.println(user);
        return user;
    }
}
